﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using Batman.Core;
using Batman.Jianbei.Forum.Core.ChatRoom;
using Batman.Jianbei.Forum.Core.Domains.Commons;
using Microsoft.EntityFrameworkCore;
using MongoDB.Driver;
using System;
using System.Linq;

namespace Batman.Jianbei.Forum.Services.ChatRoom
{
    /// <summary>
    /// 匿名身份分配服务
    /// </summary>
    public class RoomMaskService : ApplicationService
    {
        public IRepository<RoomMask, string> roomMaskRepo { get; set; }

        public IRepository<Mask, string> maskRepo { get; set; }
        public IRepository<Image, string> ImageRepo { get; set; }

        public MaskCacheService MaskCacheService { get; set; }

        public static object Mutex = string.Empty; // 锁

        /// <summary>
        /// 分配匿名身份
        /// </summary>

        /// <param name="uid"></param>
        /// <param name="roomId"></param>
        /// <returns></returns>
        public RoomMask GetOrDistributeMask(string uid, string roomId, out Mask mask)
        {
            lock (Mutex)
            {
                var distributedRoomMask = roomMaskRepo.GetQuery()
                                                      .Include(t => t.Mask)
                                                      .ThenInclude(t => t.Avatar)
                                                      .FirstOrDefault(t => t.RoomId == roomId && t.Uid == uid);
                // 如果已分配,则返回
                if (distributedRoomMask != null)
                {
                    mask = distributedRoomMask.Mask;
                    if (!distributedRoomMask.IsInRoom)
                        roomMaskRepo.Update(distributedRoomMask.Id, t =>
                        {
                            t.IsInRoom = true;
                            t.CheckIn = DateTime.Now; // 记录最后报到时间
                        }); // 更新为在房间

                    return distributedRoomMask;
                }

                // 查询已分配的匿名身份编号
                var distributedMaskIds = roomMaskRepo.GetQuery()
                                                     .Where(t => t.RoomId == roomId)
                                                     .Select(t => t.MaskId)
                                                     .ToList();

                var masksNoDistributed = MaskCacheService.GetMasks()
                                                         .Where(t => !distributedMaskIds.Contains(t.Id));
                var total = masksNoDistributed.Count(); // 未分配的数量

                if (total == 0)
                {
                    mask = new Mask();
                    return null;
                }
                // 随机获取一个数据
                var randowm = new Random(DateTime.Now.Millisecond);
                var skip = randowm.Next(0, total - 1);
                mask = masksNoDistributed.Skip(skip).FirstOrDefault();

                var newRoomMask = new RoomMask(roomId, mask.Id, uid, true);

                roomMaskRepo.Insert(newRoomMask); // 写入记录

                return newRoomMask;
            }
        }

        /// <summary>
        /// 标记为离开房间
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="uid"></param>
        /// <param name="mask"></param>
        /// <returns></returns>
        public RoomMask MarkMaskLeave(string roomId, string uid, out Mask mask)
        {
            var roomMask = roomMaskRepo.GetQuery()
                .Include(t => t.Mask)
                .ThenInclude(t => t.Avatar)
                .FirstOrDefault(t => t.RoomId == roomId && t.Uid == uid);
            mask = roomMask?.Mask;

            if (roomMask?.IsInRoom == true)
                roomMaskRepo.Update(roomMask.Id, t => t.IsInRoom = false); // 设置为不再房间状态


            return roomMask;
        }
    }
}
